import Vue from 'vue';
import VueRouter from 'vue-router';
import iView from 'view-design';

import util from '@/libs/util';

import Setting from '@/setting';

import store from '@/store/index';

// 路由数据
import routes from './routes';
import { inMenu} from '@/libs/system';
Vue.use(VueRouter);

// 导出路由 在 main.js 里使用
const router = new VueRouter({
    routes,
    mode: Setting.routerMode,
    base: Setting.routerBase
});

const originalPush = VueRouter.prototype.push;
// 修改原型对象中的push方法
VueRouter.prototype.push = function push (location) {
    return originalPush.call(this, location).catch(err => {
 return err; 
});
};
/**
 * 路由拦截
 * 权限验证
 */

function filterAddress (addressF, address) {
    addressF.forEach(ee => {
        if (ee.children && ee.children.length) {
            filterAddress(ee.children, address);
        } else {
            address.push(ee.path);
        }
    });
}

function redirectUrl (title) {
    var address = [];
    let headerId = JSON.parse(localStorage.getItem('menus')).header.find(
        ee => {
 return ee.title === title; 
}
    );
    if (headerId) {
        let headerIds = headerId.name;
        let menus = JSON.parse(localStorage.getItem('menus')).sider.filter(
            ee => {
 return ee.header === headerIds; 
}
        )[0];
        filterAddress([menus], address);
        return address;
    } else {
        return [];
    }
}
router.beforeEach((to, from, next) => {
    
    if (to.fullPath === '/monthReport') {
        if (Setting.apiBaseURL === 'https://beacon.api.jgyljt.com') {
            window.open('https://beacon.jgyljt.com/monthReport/#/monthlyplan/planList');
        } else {

        } if (Setting.apiBaseURL === 'http://dengta-test.api.jgyljt.com') {
            window.open('http://dengta-test.jgyljt.com/monthReport/#/monthlyplan/planList');
        }
        router.push({ path: from.fullPath });
        return;
    }
    
    if (Setting.showProgressBar) iView.LoadingBar.start();
    // 判断是否需要登录才可以进入
    if (to.matched.some(_ => {
 return _.meta.auth; 
})) {
        // 这里依据 token 判断是否登录，可视情况修改
        const token = util.cookies.get('token');
        if (token && token !== 'undefined') {
            let menus;
            menus = JSON.parse(localStorage.getItem('menus'));
            if(!inMenu(to.path, menus.sider)){ 
                router.push({
                    path: '/business/dashboard/todoList/visit',
                });
                store.commit('admin/page/closeTab'); 
                return;        
            }
            if (to.path === '/setting') {
                let address = redirectUrl('设置')[0]; 
                next({
                    path: address
                });
            } else if (to.path === '/business/dashboard') {
                
                if (redirectUrl('业务').length) {
                    let address = redirectUrl('业务')[0];
                    router.push({ path: address });
                    
                    if(from.path==address){
                        if (Setting.showProgressBar) iView.LoadingBar.finish();
                    }
                } else {
                    // iView.Message.message('warning', '无当前页面权限');
                    router.push({ path: '/business/dashboard/todoList/visit' });
                    if(from.path=='/business/dashboard/todoList/visit'){
                        if (Setting.showProgressBar) iView.LoadingBar.finish();
                    }
                }
                
            } else if (to.path === '/report') {
                let address = redirectUrl('报表')[0];
                next({
                    path: address
                });
            } else if (to.path === '/weChart') {
                let address = redirectUrl('微信')[0];
                next(address);
            } else { 
                next();      
                
            }
        } else {
            // 没有登录的时候跳转到登录界面
            // 携带上登陆成功之后需要跳转的页面完整路径
            next({
                name: 'login',
                query: {
                    redirect: to.fullPath
                }
            });
        }
    } else {
        // 不需要身份校验 直接通过
        next();
    }
});

router.afterEach(to => {    
    if (Setting.showProgressBar) iView.LoadingBar.finish();
    // 多页控制 打开新的页面
    store.dispatch('admin/page/open', to);
    // 更改标题
    if (to.name == 'diseases') {
        if (to.params.id == 2) {
            util.title({ title: '白癜风' });
        } else if (to.params.id == 3) {
            util.title({ title: '白斑待查' });
        }
    } else {
 util.title({ title: to.meta.title }); 
}
    // 返回页面顶端
    window.scrollTo(0, 0);
});

export default router;
